package cn.itcast.ft.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.itcast.controller.IpBean;
import cn.itcast.ft.dto.*;
import cn.itcast.ft.entity.FTToken;
import cn.itcast.utils.DingDingUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;

@RestController
@Slf4j
public class FataController {
    private static List<IpBean> list=new ArrayList<>();
    private static List<FTToken> tokenList=new ArrayList<>();
    @RequestMapping("/fataCreateMarket")
    public String createMarket(@RequestParam(value = "num",defaultValue = "10000") Integer num,
                               @RequestParam(value = "salePrice",defaultValue = "29") String salePrice,
                               @RequestParam(value = "goodsId",defaultValue = "24") String goodsId) {

        for(int i=0;i<num;i++){
            try {
                ThreadUtil.execAsync(() -> {
                    saveOrder(goodsId);
                });
                Thread.sleep(3000);
            } catch (Exception e) {
                log.error("#####异常####" + e.getMessage());
            }
        }
        log.info("####FT执行完成#####");
        return null;
    }
//    public void saveOrder(String goodsId){
//        String result=null;
//        try{
//            JSONObject jsonObject=new JSONObject();
//            jsonObject.putOnce("result","");
//            jsonObject.putOnce("node_type","product");
//            jsonObject.putOnce("sku_id",goodsId);
//            jsonObject.putOnce("wallet","HF");
//            jsonObject.putOnce("url","pages/market/details?id=85&type=product&hidene=0&number=1");
//            result = HttpRequest.post("https://fata1.fataverse.com/asset/quickBuy")
//                    .body(jsonObject.toString())
//                    .header("Authorization", "9c7e9a5cabc924dc59c198769207358d")
//                    .execute().body();
//            FataDTO fataDTO=JSONUtil.toBean(result,FataDTO.class);
//            if(fataDTO!=null){
//                String decodedStr = URLDecoder.decode(fataDTO.getMessage(), Charset.forName("UTF-8"));
//                log.info("##返回结果##"+decodedStr+"###code#"+fataDTO.getCode());
//            }
//        }catch (Exception e){
//            log.error("##token数据异常###:"+e.getMessage()+"####返回数据：###"+result);
//        }
//    }
    public void saveOrder(String goodsId){
        String result=null;
        try{
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("access_open_id","c980811702165988e54b06115d4ba827");
            jsonObject.putOnce("access_token","8440adcd583418e99c1d608870bbc499");
            jsonObject.putOnce("product_id",1);
            result = HttpRequest.post("https://manqi.peylj.com//v1/mall/buy")
                    .body(jsonObject.toString())
                    .execute().body();
            log.info("##返回结果##"+result);
            FataDTO fataDTO=JSONUtil.toBean(result,FataDTO.class);
            if(fataDTO!=null && fataDTO.getCode()>0){
                MailUtil.send("1027060531@qq.com", "收到订单", "您有一个订单，请赶紧支付！", false);
            }
        }catch (Exception e){
            log.error("##token数据异常###:"+e.getMessage()+"####返回数据：###"+result);
        }
    }
}
